import Vue from 'vue'
import Vuex from 'vuex'
import axios from '@/libs/axios'
import cookie from '@/libs/cookie'
import api from '@/libs/api'

import address from './modules/temp'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    userInfo: {},
    Company: {},
    employee: {},
    needLoadingRequestCount: 0
  },
  mutations: {
    setUserInfo (state, data) {
      state.userInfo = data
      state.employee = data.employee
    },
    upadeteLoadingRequestCount (state, n) {
      state.needLoadingRequestCount += n
    },
    setCompany (state, data) { // 获取完整公司信息
      state.Company = data
    },
    updateCompany (state, data) { // 更改公司logo和名称
      state.Company.CompanyLogo = data.logo
      state.Company.CompanyName = data.name
    }
  },
  actions: {
    getUserInfo ({ state, commit }) {
      return new Promise((resolve, reject) => {
        axios.post(api.GetEmployeeInfoLogin).then(res => {
          resolve(res)
        }).catch(err => {
          reject(err)
        })
      })
    },
    getCompany ({ state, commit }) {
      return new Promise((resolve, reject) => {
        axios.post('/DPM/Company/GetCompany').then(res => {
          resolve(res)
        }).catch(err => {
          reject(err)
        })
      })
    },
    getStamp ({ state, commit }) {
      return new Promise((resolve, reject) => {
        axios.post('/common/tool/TimeStamp').then(res => {
          resolve(res)
        }).catch(err => {
          reject(err)
        })
      })
    },
    logout ({ state, commit }) {
      return new Promise((resolve, reject) => {
        axios.post('/Account/Logout/Logout').then(res => {
          // 记住密码
          try {
            cookie.clear()
          } catch (err) {
            console.log(err)
          }
          resolve(res)
        }).catch(err => {
          reject(err)
        })
      })
    }
  },
  modules: {
    address
  }
})
